package com.ruoyi.performance.service.impl;

import com.google.common.base.Joiner;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.performance.domain.PerfLevelTemplate;
import com.ruoyi.performance.domain.PerfMitemDitem;
import com.ruoyi.performance.domain.PerfTemplateDitem;
import com.ruoyi.performance.dto.PerfTemplateDitemDto;
import com.ruoyi.performance.mapper.PerfTemplateDitemMapper;
import com.ruoyi.performance.service.PerfTemplateDitemService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author YL
 * @date 2020/8/26 11:30
 * @description 绩效模板细项Service接口实现类
 */
@Service
@Slf4j
public class PerfTemplateDitemServiceImpl implements PerfTemplateDitemService {
    @Resource
    private PerfTemplateDitemMapper perfTemplateDitemMapper;

    /**
     * 查询绩效模板细项
     *
     * @param id 绩效模板细项ID
     * @return 绩效模板细项
     */
    @Override
    public PerfTemplateDitemDto selectPerfTemplateDitemById(int id) {
        return perfTemplateDitemMapper.selectPerfTemplateDitemById(id);
    }

    /**
     * 查询绩效模板细项
     *
     * @param companyCode 公司代码
     * @return 绩效模板细项
     */
    @Override
    public List<PerfTemplateDitemDto> selectPerfTemplateDitemByCompanyCode(String companyCode) {
        return perfTemplateDitemMapper.selectPerfTemplateDitemByCompanyCode(companyCode);
    }

    /**
     * 查询绩效模板细项列表
     *
     * @param perfTemplateDitemDto 绩效模板细项
     * @return 绩效模板细项集合
     */
    @Override
    public List<PerfTemplateDitemDto> selectPerfTemplateDitemList(PerfTemplateDitemDto perfTemplateDitemDto) {
    	// 拆分状态字符串
		List<String> statuslist = perfTemplateDitemDto.getStatuslist();
		if (statuslist != null && statuslist.size() > 0) {
			String status = "'" + StringUtils.join(statuslist, "','") + "'";
			perfTemplateDitemDto.setStatus(status);
		}
    	return perfTemplateDitemMapper.selectPerfTemplateDitemList(perfTemplateDitemDto);
    }

    /**
     * 新增绩效模板细项
     *
     * @param perfTemplateDitem 绩效模板细项
     * @return 结果
     */
    @Override
    public int insertPerfTemplateDitem(PerfTemplateDitem perfTemplateDitem) {
        String username = ServletUtils.getRequest().getHeader(Constants.CURRENT_USERNAME);
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        perfTemplateDitem.setCreateBy(username);
        perfTemplateDitem.setCreateTime(now);
        perfTemplateDitem.setStatus("0");
        return perfTemplateDitemMapper.insertPerfTemplateDitem(perfTemplateDitem);
    }

    /**
     * 修改绩效模板细项
     *
     * @param datas 绩效模板细项
     * @return 结果
     */
    @Override
    public String updatePerfTemplateDitem(Map<String, List<PerfTemplateDitem>> datas) {

        
        String result = "1";
		List<String> checkstatus = new ArrayList<String>();
    	for (PerfTemplateDitem perfTemplateDitem : datas.get("datas"))
    	{
            String username = ServletUtils.getRequest().getHeader(Constants.CURRENT_USERNAME);
            String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            perfTemplateDitem.setUpdateBy(username);
            perfTemplateDitem.setUpdateTime(now);
        try {
        	perfTemplateDitemMapper.updatePerfTemplateDitem(perfTemplateDitem);
		} catch (Exception e) {
			result = perfTemplateDitem.getDitemCode() + "绩效主项更改状态失败，请联系管理员！";
			log.error(result, e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return result;
		}
    }
		if (checkstatus.size() > 0 && checkstatus != null) {
			result = Joiner.on(",").join(checkstatus);
		}
    	return result;
    }

    /**
     * 删除绩效模板细项
     *
     * @param ids 绩效模板细项ID
     * @return 结果
     */
    @Override
    public String deletePerfTemplateDitemById(String ids) {
        String result = "1";
		List<String> checkstatus = new ArrayList<String>();
		String[] tmpids = ids.split(",");
		for (String tmpid : tmpids) {
			int id = Integer.parseInt(tmpid);
			perfTemplateDitemMapper.deletePerfTemplateDitemById(id);
		}
		if (checkstatus.size() > 0 && checkstatus != null) {
			result = Joiner.on(",").join(checkstatus);
		}
		return result;
    }

    /**
     * 添加绩效主项与细项关联
     *
     * @param perfMitemDitem 关联参数
     * @return 结果
     */
    @Override
    public int insertPerfMitemDitem(PerfMitemDitem perfMitemDitem){
        return perfTemplateDitemMapper.insertPerfMitemDitem(perfMitemDitem);
    }

    /**
     * 删除绩效主项与细项关联
     * @param perfMitemDitem 关联对象
     * @return 结果
     */
    @Override
    public String deletePerfMitemDitem(Map<String, List<PerfMitemDitem>> datas){
    	String result = "1";
    	List<String> checkstatus = new ArrayList<String>();
    	for (PerfMitemDitem perfMitemDitem : datas.get("datas"))
    	{
    	   try {
			perfTemplateDitemMapper.deletePerfMitemDitem(perfMitemDitem);
		} catch (Exception e) {
			result = "绩效模板主项id" + perfMitemDitem.getMitemId() + "子项id" + perfMitemDitem.getDitemId() + "关系删除失败,请联系管理员！";
			log.error(result, e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return result;
		}

		}
		if (checkstatus.size() > 0 && checkstatus != null) {
			result = Joiner.on(",").join(checkstatus);
		}
    	return result;
    }
}
